package com.hzhk.happyrent.wxapi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.hzhk.happyrent.App;
import com.hzhk.happyrent.ui.activity.ResultPayActivity;
import com.hzhk.happyrent.ui.activity.order.OrderListActivity;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


/**
 * Created by Administrator on 2017/4/6.
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	private IWXAPI api;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		api = WXAPIFactory.createWXAPI(this, "wx08e1ba509b4f07ea");
		api.registerApp("wx08e1ba509b4f07ea");
		api.handleIntent(getIntent(), this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	/**
	 * 得到支付结果回调
	 */

	@Override
	public void onResp(BaseResp resp) {
		if (resp.errCode == 0) {
			Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(WXPayEntryActivity.this, ResultPayActivity.class);
			intent.putExtra("rent_total", App.getActivity("PayChooseActivity").getIntent().getStringExtra("rent_total"));
			startActivity(intent);
			App.destoryActivity();
		} else if (resp.errCode == -1) {
			Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();
			startActivity(new Intent(this,OrderListActivity.class));
			App.destoryActivity();
		} else if (resp.errCode == -2) {
			Toast.makeText(this, "支付取消", Toast.LENGTH_SHORT).show();
			startActivity(new Intent(this,OrderListActivity.class));
			App.destoryActivity();
		}
		finish();
	}
}